package com.dycx.api.controller.manage.food;

import com.dycx.common.RestResponse;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.food.dto.GoodsDTO;
import com.dycx.food.dto.GoodsQueryDTO;
import com.dycx.food.service.GoodsMainFoodService;
import com.dycx.food.service.GoodsFoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/manage/food/goods")
public class GoodsFoodController {

    @Resource
    private GoodsFoodService goodsService;

    @Resource
    private GoodsMainFoodService goodsMainService;


    /**
     * 添加
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_GOODS, bizValue = "{#goodsDto.id}")
    @PostMapping("/addGoods")
    public RestResponse<?> addGoods(@RequestBody @Valid GoodsDTO goodsDto) {
        return goodsService.addGoods(goodsDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 修改
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_GOODS, bizValue = "{#goodsDto.id}")
    @PostMapping("/updateGoods")
    public RestResponse<?> updateGoods(@RequestBody @Valid GoodsDTO goodsDto) {
        return goodsService.updateGoods(goodsDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 刪除
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_GOODS, bizValue = "{#mainGoodsId}")
    @GetMapping("/deleteGoods")
    public RestResponse<?> deleteGoods(int mainGoodsId) {
        return goodsService.deleteGoods(mainGoodsId) ? RestResponse.success() : RestResponse.fail();
    }


    /**
     * 分页
     **/
    @PostMapping("/getPage")
    public RestResponse<?> getPage(@RequestBody GoodsDTO goodsDto) {
        return RestResponse.success(goodsService.getPage(goodsDto));
    }

    /**
     * 获取公共产品分页
     **/
    @PostMapping("/getGoodsMainPage")
    public RestResponse<?> getGoodsMainPage(@RequestBody GoodsQueryDTO goodsDto) {
        return RestResponse.success(goodsMainService.getPage(goodsDto));
    }

    @GetMapping("/getGoodsMainDetail")
    public RestResponse<?> getGoodsMainDetail(int mainGoodsId) {
        return RestResponse.success(goodsService.getGoodsMainDetail(mainGoodsId));
    }

    @WriterLog(bizType = BizTypeEnum.MALL_GOODS, bizValue = "{#goodsDto.id}")
    @PostMapping("/updateShelf")
    public RestResponse<?> updateShelf(@RequestBody GoodsDTO goodsDto) {
        return goodsService.updateShelf(goodsDto) ? RestResponse.success() : RestResponse.fail();
    }

    @PostMapping("/batchUpdateGoodsPrice")
    public RestResponse<?> batchUpdateGoodsPrice(HttpServletRequest request) {
        return goodsService.batchUpdateGoodsPrice(request) ? RestResponse.success() : RestResponse.fail();

    }


    /**
     * 修改排序
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_GOODS, bizValue = "{#goodsDTO.id}")
    @PostMapping("/updateSort")
    public RestResponse<?> updateSort(@RequestBody GoodsDTO goodsDTO) {
        return goodsMainService.updateSort(goodsDTO) ? RestResponse.success() : RestResponse.fail();
    }

}
